
using Prometheus.Binding.Validation.Rules;
#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using  Windows.UI.Xaml.Media;
using Windows.UI;
#else
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
#endif
namespace Prometheus.Binding.Validation
{
    public interface IErrorBorder
    {
        void SetError(proValidationException firstError);
    }

    /// <summary>
    /// Wrap any control in it to allow proValidationBinding to show error
    /// </summary>
    public class proErrorBorder : ContentControl, IErrorBorder
    {
        public proErrorBorder()
        {
            DefaultStyleKey = typeof (proErrorBorder);
            ToolTip = new ToolTip {Placement = PlacementMode.Right};
        }

        public static readonly DependencyProperty ToolTipProperty = DependencyProperty.Register("ToolTip",
                                                                                                typeof (ToolTip),
                                                                                                typeof (proErrorBorder),
                                                                                                new PropertyMetadata(new ToolTip {Placement = PlacementMode.Right}));
        
        public ToolTip ToolTip
        {
            get { return (ToolTip)GetValue(ToolTipProperty); }
            set {SetValue(ToolTipProperty,value);}
        }

        static proErrorBorder()
        {
            ErrorBorderBrush = new SolidColorBrush(Colors.Red);
            WarningBorderBrush = new SolidColorBrush(new Color { R = 255, G = 165, B = 0, A = 255 });
            DefaultBorderBrush = new SolidColorBrush(Colors.Transparent);
        }

        /// <summary>
        /// Brush for error
        /// </summary>
        private static readonly Brush ErrorBorderBrush;

        /// <summary>
        /// Brush for warning
        /// </summary>
        private static readonly Brush WarningBorderBrush;

        private static readonly Brush DefaultBorderBrush;

        public void SetError(proValidationException firstError)
        {
            ToolTip.DataContext = firstError;
            if (firstError == null)
            {
                BorderBrush = DefaultBorderBrush;
            }
            else
            {
                BorderBrush = firstError.Type == proValidationResultType.Error
                                              ? ErrorBorderBrush
                                              : WarningBorderBrush;
            }
        }
    }
}